package yxgf.oem.oemquotemgt.mservice.service.impl;

import com.alibaba.nacos.api.utils.StringUtils;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.openapi.common.result.CustomApiResult;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.QueryServiceHelper;
import yxgf.oem.oemquotemgt.commons.dto.PriceDto;
import yxgf.oem.oemquotemgt.commons.enums.StockTypeEnum;
import yxgf.oem.oemquotemgt.mservice.service.IPriceService;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;

/**
 * @description：
 * @version: v1.0
 * @author: Junit
 * @date: 2025/5/14
 */
public class PriceServiceImpl implements IPriceService {

    /**
     * 获取预测价
     *
     * @param orgCode
     * @param materialCode
     * @param stockType
     * @return
     */
    @Override
    public PriceDto getForecastPrice(String orgCode, String materialCode, String stockType) {
        QFilter orgFilter = new QFilter("yxgf_orgfield1.number", QCP.equals, orgCode);
        QFilter matFilter = new QFilter("entryentity.yxgf_materielfield.number", QCP.equals, materialCode);
        QFilter filter = orgFilter.and(matFilter);

        DynamicObjectCollection forDynObjCollection = QueryServiceHelper.query("yxgf_pur_forecast_result",
                "entryentity.yxgf_materielfield.number," +
                        "entryentity.yxgf_ycscj," +
                        "entryentity.yxgf_bydkcdj," +
                        "entryentity.yxgf_xydkcj," +
                        "createtime", new QFilter[]{filter}, "createtime desc");

        PriceDto priceRes = new PriceDto();
        if (forDynObjCollection.size() > 0) {
            Object obj = forDynObjCollection.get(0).get(1);
            BigDecimal forPrice = (forDynObjCollection.size() > 0) && (obj != null) ? new BigDecimal(obj.toString()) : BigDecimal.ZERO;
            priceRes.setForPrice(forPrice);

            // stock
            if (StringUtils.equals(StockTypeEnum.MARKET_SEC_HALF_MONTH.getCode(), stockType)) {
                Object obj3 = forDynObjCollection.get(0).get(3);
                BigDecimal secondMPrice = (forDynObjCollection.size() > 0) && (obj3 != null) ? new BigDecimal(obj3.toString()) : BigDecimal.ZERO;
                priceRes.setStockPrice(secondMPrice);
            } else {
                Object obj2 = forDynObjCollection.get(0).get(2);
                BigDecimal firstMPrice = (forDynObjCollection.size() > 0) && (obj2 != null) ? new BigDecimal(obj2.toString()) : BigDecimal.ZERO;
                priceRes.setStockPrice(firstMPrice);
            }
        }
        return priceRes;
    }

    /**
     * 获取友商价
     *
     * @param compCode
     * @param materialCode
     * @return
     */
    @Override
    public BigDecimal getCompPrice(String compCode, String materialCode) {
        QFilter orgFilter = new QFilter("yxgf_crmcus.number", QCP.equals, compCode);
        QFilter matFilter = new QFilter("yxgf_entryentity.yxgf_materielfield.number", QCP.equals, materialCode);
        QFilter filter = orgFilter.and(matFilter);

        DynamicObjectCollection comObjCollection = QueryServiceHelper.query("yxgf_competitorprice",
                "yxgf_entryentity.yxgf_materielfield.number," +
                        "yxgf_entryentity.yxgf_cpprice," +
                        "createtime", new QFilter[]{filter}, "createtime desc");

        return ((comObjCollection.size() > 0) && (comObjCollection.get(0).get(1) != null)) ?
                new BigDecimal(comObjCollection.get(0).get(1).toString()) : BigDecimal.ZERO;
    }

    /**
     * 获取结存价
     *
     * @param materialCode
     * @param busDate
     * @return
     */
    @Override
    public String getFinancePriceInfo(String orgCode, String materialCode, Date busDate) {
        QFilter orgFilter = new QFilter("yxgf_material.number", QCP.equals, orgCode);
        QFilter matFilter = new QFilter("yxgf_material.number", QCP.equals, materialCode);
        QFilter dateFilter = new QFilter("yxgf_tallydate", QCP.equals, busDate);
        QFilter filter = orgFilter.and(matFilter).and(dateFilter);

        DynamicObjectCollection finObjCollection = QueryServiceHelper.query("yxgf_calcurrenstockgather",
                "yxgf_material.number,yxgf_warehouse,yxgf_endingprice,yxgf_tallydate",
                new QFilter[]{filter}, "yxgf_tallydate desc");

        Iterator<DynamicObject> iterator = finObjCollection.iterator();
        StringBuffer finMsg = new StringBuffer();
        if (finObjCollection.size() > 0) {
            iterator.forEachRemaining(tem -> {
                finMsg.append(tem.getString(1))
                        .append(",")
                        .append(tem.getString(2))
                        .append(";");
            });
        }
        return StringUtils.isBlank(finMsg.toString()) ? "仓库1,120;仓库2:100" : finMsg.toString();
    }

    /**
     * 获取价格
     *
     * @param orgCode
     * @param materialCode
     * @param stockType
     * @param compCode
     * @return
     */
    @Override
    public CustomApiResult<PriceDto> getPriceList(String orgCode, String materialCode, String stockType, String compCode) {
        CustomApiResult<PriceDto> result = new CustomApiResult<>();
        PriceDto priceDto = getForecastPrice(orgCode, materialCode, stockType);
        priceDto.setCompPrice(getCompPrice(compCode, materialCode));

        priceDto.setFinancePrice(getFinancePriceInfo(orgCode, materialCode, new Date()));
        result.setData(priceDto);
        return result;
    }

}
